package dominion.model.actions.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import dominion.model.Card;
import dominion.model.GameBoard;
import dominion.model.Player;
import dominion.model.actions.Action;
import dominion.model.monies.Coin;
import dominion.utils.Constants;

public class Thief extends Action {

	public Thief() {
		cost = 4;
	}

	@Override
	public Card clone() {
		return new Thief();
	}

	@Override
	public String toString() {
		return "Thief";
	}

	@Override
	public String getDescription() {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDescription());
		sb.append("Each player reveals the top 2 cards of his deck.\n");
		sb.append("If they revealed any treasure cards, they trash one of them that you choose.\n");
		sb.append("You may gain any or all of the trashed cards.\n");
		sb.append("They discard the other revealed cards.\n");
		return sb.toString();
	}

	@Override
	public void doAction(Player player, GameBoard board, List<Player> players) throws IOException {
		player.changeAction(-1);
		playedCard(player);

		for (Player target : players)
		{
			if (target.equals(player))
			{
				continue;
			}
			if (target.hasCounter())
			{
				Constants.printer.print(target.getName() + " has Moat and is unaffected.");
				continue;
			}

			Card card1 = target.drawTopCardFromDeck(Constants.LIBRARY_DECK);
			Card card2 = target.drawTopCardFromDeck(Constants.LIBRARY_DECK);
			Constants.printer.print(target.getName() + ": |" + card1.toString() + "||" + card2.toString() + "|");
			if (!(card1 instanceof Coin) && !(card2 instanceof Coin))
			{
				Constants.printer.print("Nothing to trash.");
				target.addToDeck(card1, Constants.DISCARD_DECK);
				target.addToDeck(card2, Constants.DISCARD_DECK);
			}
			else
			{
				Constants.printer.print("Pick a card to trash");
				String input = null;
				Card choice = null;
				BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
				while (true)
				{
					input = in.readLine().trim().toLowerCase();
					if (input.equals(card1.toString().toLowerCase()) && card1 instanceof Coin)
					{
						choice = card1;
						target.addToDeck(card2, Constants.DISCARD_DECK);
						break;
					}
					else if (input.equals(card2.toString().toLowerCase()) && card2 instanceof Coin)
					{
						choice = card2;
						target.addToDeck(card1, Constants.DISCARD_DECK);
						break;
					}
					else
					{
						Constants.printer.print("Invalid selection. Must select a Treasure.");
					}
				}

				Constants.printer.print("Trash or keep the card?");
				while (true)
				{
					input = in.readLine().trim().toLowerCase();
					if (input.equals("keep"))
					{
						player.addToDeck(choice, Constants.DISCARD_DECK);
						Constants.printer.print(choice.toString() + " is added to your discard.");
						break;
					}
					else if (input.equals("trash"))
					{
						board.addToTrash(choice);
						Constants.printer.print(choice.toString() + " has been trashed.");
						break;
					}
					else
					{
						Constants.printer.print("Invalid input");
					}
				}
			}
		}
	}
}
